﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;

namespace QLHH
{
    /// <summary>
    /// Interaction logic for DSHoaDonWindow.xaml
    /// </summary>
    public partial class DSHoaDonWindow : Window
    {
        HoaDon hd = new HoaDon();


        public DSHoaDonWindow()
        {
            InitializeComponent();
        }

        private void mnNew_Click(object sender, RoutedEventArgs e)
        {
            DataView dv = (DataView)datagrid.ItemsSource;
            DataRowView row = dv.AddNew();
            HoaDonWindow hdw = new HoaDonWindow(row, true);
            hdw.ShowDialog();
            row.EndEdit();
            datagrid.Items.Refresh();
        }
        private void btEdit_Click(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)datagrid.SelectedItem;
            HoaDonWindow hdw = new HoaDonWindow(row, false);
            hdw.ShowDialog();
            row.EndEdit();
            datagrid.Items.Refresh();
        }
        private void btClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            datagrid.SelectedValuePath = "sohd";
            datagrid.ItemsSource = hd.DanhSachHoaDon().DefaultView;
        }

        private void btDelete_Click(object sender, RoutedEventArgs e)
        {

            string shd = datagrid.SelectedValue.ToString();
            MessageBoxResult result = MessageBox.Show("Bạn có muốn xóa hóa đơn " + shd + " không ?", "Thống báo", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
            {

                hd.XoaHoaDon(shd);

                DataRowView rowSelect = (DataRowView)datagrid.SelectedItem;
                rowSelect.Delete();
            }
        }

        private void btPrint_Click(object sender, RoutedEventArgs e)
        {
            DataTable tb = hd.DanhSachHoaDon();
            tb.TableName = "DSHoaDon";
            DataSet ds = new DataSet();
            ds.DataSetName = "dsHoaDon";
            ds.Tables.Add(tb);
            ReportDocument report = new ReportDocument();
            report.Load("..\\..\\DSHoaDon.rpt");
            report.SetDataSource(ds);
            ReportViewer rv = new ReportViewer(report);
            rv.ShowDialog();

        }


    }
}
